import { crtChannelHw } from "./crtChannelHw";
import { crtChannelKs } from "./crtChannelKs";
import { crtChannelKs1 } from "./crtChannelKs1";
import { crtChannelKs2 } from "./crtChannelKs2";
import { crtChannelKs3 } from "./crtChannelKs3";
import { crtChannelKs5 } from "./crtChannelKs5";
import { crtChannelOppo } from "./crtChannelOppo";
import { crtChannelVivo } from "./crtChannelVivo";
import { crtChannelWeb } from "./crtChannelWeb";

/**
 * 分渠道打包处理,出包时配置其中的channel值为渠道对象
 *
 */
export class crtChannel {

    /** 当前渠道,出包在此配置渠道, 仅当remoteVerKey为debug时有效否则会变成web  */
    static channel: string = 'hw';
    static channelMap: Map<String, crtChannelWeb> = new Map([
        ['oppo', new crtChannelOppo()],
        ['web', new crtChannelWeb()],
        ['vivo', new crtChannelVivo()],
        ['ks', new crtChannelKs()],
        ['ks1', new crtChannelKs1()],
        ['ks2', new crtChannelKs2()],
        ['ks3', new crtChannelKs3()],
        ['ks5', new crtChannelKs5()],
        ['hw', new crtChannelHw()],
    ]);


    static getChannel(): crtChannelWeb {
        return crtChannel.channelMap.get(crtChannel.channel);
    }


    static exit() {
        crtChannel.getChannel().exit();
    }

    /** getSystemInfoSync */
    static getSysInfo(): any {
        return crtChannel.getChannel().getSysInfo();
    }

    /* 是否有隐私政策 */
    static hasPrivacy(): boolean {
        return crtChannel.getChannel().hasPrivacy();
    }
    static privacyReject(): number {
        return crtChannel.getChannel().privacyReject();
    }
    /** 同意隐私政策 */
    static privacyAgree(): number {
        return crtChannel.getChannel().privacyAgree();
    }
    static privacyNextPage(pageType: number): string {
        return crtChannel.getChannel().privacyNextPage(pageType);
    }
    static privacyPrePage(pageType: number): string {
        return crtChannel.getChannel().privacyPrePage(pageType);
    }
    static privacyPage1(pageType: number): string {
        return crtChannel.getChannel().privacyPage1(pageType);
    }


    static init() {
        console.error("channel init: crtChannel");
        //注意web渠道不会调loginCall
        crtChannel.getChannel().init();
    }

    public static toast(txt: string, startY: number = 0) {
    }

    static showPrivacy(isToMain: boolean) {
    }


    static jpost(url: string, data: any, callback?: any) {
        if (!callback) {
            callback = function (err) {
                if (err) {
                    return console.error(err);
                }
            };
        }
        const xhr = new XMLHttpRequest();
        // const now = new Date().getTime();
        xhr.open('POST', url, true);
        xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
        const postData = JSON.stringify(data);
        // geckc.log('post url:' + url);
        // geckc.log('post j:' + postData);
        xhr.onload = function () {
            if (xhr.status == 200) {
                let j = { 'postERR': '' + xhr.responseText };
                try {
                    j = JSON.parse(xhr.responseText);
                } catch (e) {
                    console.error(e);
                    return callback(null, { 'code': '1111', 'err': 'jsonErr:' + xhr.responseText });
                }
                return callback(null, j);
            } else {
                return callback(null, { 'code': '' + xhr.status });
            }
        };
        xhr.onerror = function (err) {
            callback(err);
        };
        try {
            xhr.send(postData);
        } catch (e) {
            console.error(e);
        }
    };
}
